package com.orhon.smartcampus.modules.student.mutation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.student.dao.OperatingDao;
import com.orhon.smartcampus.modules.student.dao.RewardsDao;
import com.orhon.smartcampus.modules.student.entity.Operating;
import com.orhon.smartcampus.modules.student.entity.Rewards;
import com.orhon.smartcampus.modules.student.gqlInputModel.GQLRewardsInput;
import com.orhon.smartcampus.modules.student.inputMapper.RewardsInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddRewardsMutation implements DataFetcher {
	@Autowired
	RewardsDao rewardsDao;
	@Autowired
	OperatingDao operatingDao;
	@Autowired
	TokenService tokenService;

	@Override
	public Object get(DataFetchingEnvironment environment) throws Exception {

		String token = tokenService.getSystemToken();
		Long schoolId = JwtUtils.getSchoolId(token);
		String username = JwtUtils.getUsername(token);
		Long userId = JwtUtils.getUserId(token);
		GQLRewardsInput input = RewardsInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));
		Rewards rewards = null;

		Operating operating = new Operating();
		String content = "";

		if (input.getId() != null){
			rewards = rewardsDao.get(Rewards.class , input.getId());
			rewards.setUpdated_at(new Date());
			content += "更新:";
		}
		else{
			rewards = new Rewards();
			rewards.setCreated_at(new Date());
			content += "发起:";
		}

		if(input.getStudent_id() != null){
			rewards.setStudent_id(input.getStudent_id());
		}
		if(input.getDiscipline_name() != null){
			rewards.setDiscipline_name(input.getDiscipline_name());
			content += input.getDiscipline_name();
		}
		if(input.getGrade() != null){
			rewards.setGrade(input.getGrade());
			content +=input.getGrade();
		}

		if(input.getStatus() != null){
			rewards.setStatus(input.getStatus());
			if(input.getStatus()==1) {
				operating.setOperate("已审批");
			}
			if(input.getStatus()==2) {
				operating.setOperate("驳回操作");
			}
		}else {
			rewards.setStatus(0);
			operating.setOperate("提交申请");
		}
		if(input.getRemarks() != null){
			rewards.setRemarks(input.getRemarks());
		}
		if(input.getUser_name() != null){
			rewards.setUser_name(username);
		}else {
			rewards.setUser_name(username);
		}
		operating.setContent(content);
		operating.setCreated_at(new Date());
		operating.setUser_id(Integer.parseInt(userId+""));
		operating.setUser_name(username);
		operating.setStudent_id(input.getStudent_id());

		try{
			rewardsDao.saveOrUpdate(rewards);
			operating.setRewards_id(rewards.getId());
			operatingDao.saveOrUpdate(operating);
		}
		catch (Exception e){
			return Result.error(500,""+e);
		}

		return rewards;
	}
}

